package com.ramotion.expandingcollection;

import com.ramotion.expandingcollection.pojo.CardData;
import com.ramotion.expandingcollection.pojo.Comment;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;

import java.util.List;
import java.util.Locale;
import java.util.Optional;

/**
 * SampleItemProvider
 *
 * @since 2021-04-25
 */
public class CommentArrayProvider extends BaseItemProvider {
    private Ability ability;
    private CardData ecData;
    private List<Comment> list;
    private float alpha = 1;

    /**
     * onStart
     *
     * @param ability 上下文
     * @param ecData  数据
     * @param list    数据
     */
    public CommentArrayProvider(Ability ability, CardData ecData, List<Comment> list) {
        this.ability = ability;
        this.ecData = ecData;
        this.list = list;
    }

    /**
     * getCount
     *
     * @return count
     */
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * setAlpha
     *
     * @param alpha 透明度
     */
    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }

    /**
     * getCount
     *
     * @param pos 位置
     * @return obj
     */
    @Override
    public Object getItem(int pos) {
        if (list != null && pos >= 0 && pos < list.size()) {
            return list.get(pos);
        }
        return Optional.empty();
    }

    /**
     * setIsVertical
     *
     * @param pos 位置
     * @return pos
     */
    @Override
    public long getItemId(int pos) {
        return pos;
    }

    /**
     * setIsVertical
     *
     * @param pos
     * @param component
     * @param componentContainer
     * @return cpt
     */
    @Override
    public Component getComponent(int pos, Component component, ComponentContainer componentContainer) {
        Component cpt;
        final Comment objectItem = (Comment) getItem(pos);
        if (pos == 0) {
            cpt = LayoutScatter.getInstance(ability).parse(ResourceTable.Layout_layout_header, null, false);
//            int fullWidth = ResUtils.getScreenWidth(ability);
//            StackLayout.LayoutConfig layoutParams = new StackLayout.LayoutConfig(fullWidth, ComponentContainer.LayoutConfig.MATCH_CONTENT);
//            cpt.setLayoutConfig(layoutParams);
            Image image = (Image) cpt.findComponentById(ResourceTable.Id_image);
            Text text = (Text) cpt.findComponentById(ResourceTable.Id_text);
            image.setPixelMap(ecData.getHeadBackgroundResource());
            text.setText(ecData.getHeadTitle().toUpperCase(Locale.ENGLISH));
            image.setAlpha(alpha);
        } else {
            cpt = LayoutScatter.getInstance(ability).parse(ResourceTable.Layout_list_item, null, false);
//            int fullWidth = ResUtils.getScreenWidth(ability);
//            StackLayout.LayoutConfig layoutParams = new StackLayout.LayoutConfig(fullWidth, ComponentContainer.LayoutConfig.MATCH_CONTENT);
//            cpt.setLayoutConfig(layoutParams);
            Image icon = (Image) cpt.findComponentById(ResourceTable.Id_icon);
            icon.setPixelMap(objectItem.getCommentPersonPictureRes());
            Text firstLine = (Text) cpt.findComponentById(ResourceTable.Id_firstLine);
            Text secondLine = (Text) cpt.findComponentById(ResourceTable.Id_secondLine);
            Text firstLineDate = (Text) cpt.findComponentById(ResourceTable.Id_firstLineDate);
            firstLine.setText(objectItem.getCommentPersonName() + ":");
            secondLine.setText(objectItem.getCommentText());
            firstLineDate.setText(objectItem.getCommentDate().toUpperCase(Locale.ENGLISH));
            DirectionalLayout bottom = (DirectionalLayout) cpt.findComponentById(ResourceTable.Id_bottom);
            Text text1 = (Text) cpt.findComponentById(ResourceTable.Id_text1);
            Text text2 = (Text) cpt.findComponentById(ResourceTable.Id_text2);
            Text text3 = (Text) cpt.findComponentById(ResourceTable.Id_text3);
            text1.setText(ecData.getPersonViewsCount() + "");
            text2.setText(ecData.getPersonLikesCount() + "");
            text3.setText(ecData.getPersonLikesCount() + "");
            if (pos == 1) {
                bottom.setVisibility(Component.VISIBLE);
                firstLineDate.setVisibility(Component.HIDE);
                icon.setAlpha(alpha);
            } else {
                bottom.setVisibility(Component.HIDE);
                firstLineDate.setVisibility(Component.VISIBLE);
            }
        }
        return cpt;
    }
}
